﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace pxt
{
    public class Player
    {
        // Members
        protected Stats mStats;
        protected bool mIsActive;
        protected int mTeam;


        protected TongaTimer mSpamTimer;
        protected TongaTimer mCooldownTimer;
        protected TongaTimer mReflectTimer;

        // the character this player controls
        protected Wizard mWizard;


        public Player()
        {
            mTeam = 0;
            mIsActive = false;
            mWizard = new Wizard(this);
            mSpamTimer = new TongaTimer(0.2);
            mCooldownTimer = new TongaTimer(0.0f);
            mReflectTimer = new TongaTimer(3.0f);
            mStats = new Stats("GamerTag Here");
        }

        public void Load()
        {
            mWizard.Load();
            // TODO: Load a hud
        }

        public void Update(GameTime gt)
        {
            // update our character
            if (mWizard.Alive)
            {
                mWizard.Update(gt);
                mSpamTimer.Update(gt);
                mCooldownTimer.Update(gt);
            }
        }

        public void HandleInput(InputState input, int index)
        {
            // no input - we are dead
            if (!mWizard.Alive)
                return;

            // Check for right thumbstick movement and check the spam cooldown
            if ((input.CurrentGamePadStates[index].ThumbSticks.Right.X >= .2 || input.CurrentGamePadStates[index].ThumbSticks.Right.X <= -.2 ||
                input.CurrentGamePadStates[index].ThumbSticks.Right.Y >= .2 || input.CurrentGamePadStates[index].ThumbSticks.Right.Y <= -.2) && mSpamTimer.Check())
            {
                Vector2 dir = new Vector2(input.CurrentGamePadStates[index].ThumbSticks.Right.X, -input.CurrentGamePadStates[index].ThumbSticks.Right.Y);
                dir.Normalize();
                mWizard.Shoot(projectile_type.Spam, dir);
                mSpamTimer.Reset();
            }

            // revive
            if (input.CurrentGamePadStates[index].IsButtonDown(Buttons.Y) && input.LastGamePadStates[index].IsButtonUp(Buttons.Y) && mWizard.Special)
            {
                //mWizard.Shoot(projectile_type.Revive, new Vector2(5, 0));
                mWizard.Shoot(projectile_type.Revive, Vector2.Zero);
                // check for a dead teammate
                mWizard.Special = false;
            }

            //reflect
            if (input.CurrentGamePadStates[index].IsButtonDown(Buttons.X) && input.LastGamePadStates[index].IsButtonUp(Buttons.X) && mWizard.Special)
            {
                //mWizard.Shoot(projectile_type.Revive, new Vector2(5, 0));

                mWizard.Reflect = true;
                mReflectTimer.Reset();
                // check for a dead teammate
                mWizard.Special = false;
            }

            // Charging Super!
            if (input.CurrentGamePadStates[index].IsButtonDown(Buttons.RightTrigger) &&
                input.LastGamePadStates[index].IsButtonDown(Buttons.RightTrigger))
            {
                // a frame has gone by and we are holding down the right trigger
                mWizard.ChargeSuper();
                //Console.WriteLine("Charging super!");
            }
            else if (input.CurrentGamePadStates[index].IsButtonUp(Buttons.RightTrigger) &&
                input.LastGamePadStates[index].IsButtonDown(Buttons.RightTrigger))
            {
                // we let go of the charge, reset it
                // TODO: Reset wizard charge.
                mWizard.ResetSuperCharge();
            }

            // Charging Extra Damage Spell!
            if (input.CurrentGamePadStates[index].IsButtonDown(Buttons.LeftTrigger) &&
                input.LastGamePadStates[index].IsButtonDown(Buttons.LeftTrigger))
            {
                // a frame has gone by and we are holding down the left trigger
                // TODO: Update the charge meter.
                mWizard.ChargeExtra();
                //Console.WriteLine("Charging extra!");
            }
            else if (input.CurrentGamePadStates[index].IsButtonUp(Buttons.LeftTrigger) &&
                input.LastGamePadStates[index].IsButtonDown(Buttons.LeftTrigger))
            {
                // we let go of the charge, reset it
                // TODO: Reset wizard charge.
                mWizard.ResetExtraCharge();
            }

            // Move the wizard with the left thumbstick, update scale factor later...
            mWizard.Position += (mWizard.Speed * new Vector2(input.CurrentGamePadStates[index].ThumbSticks.Left.X,
                                            -(input.CurrentGamePadStates[index].ThumbSticks.Left.Y)));
        }

        private void Shoot()
        {
            // handle shooting, check cooldowns, and special uses - then tell the wizard class what to shoot!

        }

        // make me ALIVE!!!
        public void Spawn(Vector2 pos)
        {
            mWizard.Position = pos;
            mWizard.Alive = true;
        }

        public void Draw()
        {
            // always draw the wizard, even if he is dead
            mWizard.Draw();
        }

        public int Team
        {
            get { return mTeam; }
            set { mTeam = value; }
        }

        public Wizard Wizard
        {
            get { return mWizard; }
        }

        public Stats Stats
        {
            get { return mStats; }
        }

    }
}
